import 'package:bt_dsp_88/model/model_default.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../../model/model.dart';
import '../../tool/assets.dart';

class OutputItem extends StatefulWidget {
  ChannelModel ch;
  void Function(ChannelModel) onTap;
  void Function(ChannelModel) onLongPress;
  OutputItem(
      {super.key,
      required this.ch,
      required this.onTap,
      required this.onLongPress});

  @override
  State<OutputItem> createState() => _OutputItemState();
}

class _OutputItemState extends State<OutputItem> {
  late final LongPressGestureRecognizer _longPressRecognizer;
  bool get enalbe {
    return widget.ch.enable && globalModel.input_type.isNotEmpty;
  }

  @override
  void initState() {
    _longPressRecognizer =
        LongPressGestureRecognizer(duration: const Duration(milliseconds: 2000))
          ..onLongPress = () {
            widget.onLongPress(widget.ch);
          };
    super.initState();
  }

  @override
  void dispose() {
    _longPressRecognizer.dispose();
    super.dispose();
  }

  Widget _item(String text, {bool isbold = false}) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      double fontSize = constraints.maxHeight - 4; // 动态计算字体大小

      return Center(
        child: Text(
          text,
          style: TextStyle(
              height: 0,
              fontSize: isbold ? 14 : 12,
              fontWeight: isbold ? FontWeight.bold : FontWeight.normal,
              color: widget.ch.select && !isbold ? Color_theme : Colors.black),
        ),
      );
    });
  }

  Widget _listItem(bool istop) {
    BorderSide _border(bool show) {
      return show
          ? BorderSide(
              color: enalbe ? Colors.black : Color_disEnable, // 边框颜色
              width: 0.25, // 边框宽度
            )
          : BorderSide.none;
    }

    List<int> list = istop
        ? widget.ch.input_list.sublist(0, 4)
        : widget.ch.input_list.sublist(4, 8);
    return Flexible(
        child: Row(
      children: List.generate(4, ($0) {
        return Flexible(
            child: Container(
          decoration: BoxDecoration(
            border: Border(
                top: _border(!istop),
                left: _border($0 != 0),
                right: _border($0 != 3),
                bottom: _border(istop)), // 设置圆角
          ),
          child: _item(list[$0] == 0 ? '' : '${list[$0]}'),
        ));
      }).toList(),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onLongPress: _longPressRecognizer.onLongPress,
      onTap: enalbe == false ||
              globalModel.input_type == 'Assign' ||
              widget.ch.select
          ? null
          : () => widget.onTap(widget.ch),
      child: Container(
        decoration: BoxDecoration(
          color: Color_bg_view,
          borderRadius: BorderRadius.circular(5),
          border: Border.all(
              color: enalbe ? Colors.black : Color_disEnable, width: 0.5),
        ),
        child: Column(
          children: [
            ///顶部四个等宽组件
            _listItem(true),

            ///中间一个组件
            Expanded(
                child: Container(
              color: bg_color(widget.ch),
              child: _item(widget.ch.name, isbold: true),
            )),

            ///底部四个等宽组件
            _listItem(false),
          ],
        ),
      ),
    );
  }
}
